<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>uri.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_uri_uri.js.html">uri.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2006 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Class for parsing and formatting URIs.
<a name="line17"></a> *
<a name="line18"></a> * Use goog.Uri(string) to parse a URI string.  Use goog.Uri.create(...) to
<a name="line19"></a> * create a new instance of the goog.Uri object from Uri parts.
<a name="line20"></a> *
<a name="line21"></a> * e.g: &lt;code&gt;var myUri = new goog.Uri(window.location);&lt;/code&gt;
<a name="line22"></a> *
<a name="line23"></a> * Implements RFC 3986 for parsing/formatting URIs.
<a name="line24"></a> * http://gbiv.com/protocols/uri/rfc/rfc3986.html
<a name="line25"></a> *
<a name="line26"></a> * Some changes have been made to the interface (more like .NETs), though the
<a name="line27"></a> * internal representation is now of un-encoded parts, this will change the
<a name="line28"></a> * behavior slightly.
<a name="line29"></a> *
<a name="line30"></a> */
<a name="line31"></a>
<a name="line32"></a>goog.provide(&#39;goog.Uri&#39;);
<a name="line33"></a>goog.provide(&#39;goog.Uri.QueryData&#39;);
<a name="line34"></a>
<a name="line35"></a>goog.require(&#39;goog.array&#39;);
<a name="line36"></a>goog.require(&#39;goog.string&#39;);
<a name="line37"></a>goog.require(&#39;goog.structs&#39;);
<a name="line38"></a>goog.require(&#39;goog.structs.Map&#39;);
<a name="line39"></a>goog.require(&#39;goog.uri.utils&#39;);
<a name="line40"></a>goog.require(&#39;goog.uri.utils.ComponentIndex&#39;);
<a name="line41"></a>
<a name="line42"></a>
<a name="line43"></a>
<a name="line44"></a>/**
<a name="line45"></a> * This class contains setters and getters for the parts of the URI.
<a name="line46"></a> * The &lt;code&gt;getXyz&lt;/code&gt;/&lt;code&gt;setXyz&lt;/code&gt; methods return the decoded part
<a name="line47"></a> * -- so&lt;code&gt;goog.Uri.parse(&#39;/foo%20bar&#39;).getPath()&lt;/code&gt; will return the
<a name="line48"></a> * decoded path, &lt;code&gt;/foo bar&lt;/code&gt;.
<a name="line49"></a> *
<a name="line50"></a> * The constructor accepts an optional unparsed, raw URI string.  The parser
<a name="line51"></a> * is relaxed, so special characters that aren&#39;t escaped but don&#39;t cause
<a name="line52"></a> * ambiguities will not cause parse failures.
<a name="line53"></a> *
<a name="line54"></a> * All setters return &lt;code&gt;this&lt;/code&gt; and so may be chained, a la
<a name="line55"></a> * &lt;code&gt;goog.Uri.parse(&#39;/foo&#39;).setFragment(&#39;part&#39;).toString()&lt;/code&gt;.
<a name="line56"></a> *
<a name="line57"></a> * @param {*=} opt_uri Optional string URI to parse
<a name="line58"></a> *        (use goog.Uri.create() to create a URI from parts), or if
<a name="line59"></a> *        a goog.Uri is passed, a clone is created.
<a name="line60"></a> * @param {boolean=} opt_ignoreCase If true, #getParameterValue will ignore
<a name="line61"></a> * the case of the parameter name.
<a name="line62"></a> *
<a name="line63"></a> * @constructor
<a name="line64"></a> */
<a name="line65"></a>goog.Uri = function(opt_uri, opt_ignoreCase) {
<a name="line66"></a>  // Parse in the uri string
<a name="line67"></a>  var m;
<a name="line68"></a>  if (opt_uri instanceof goog.Uri) {
<a name="line69"></a>    this.ignoreCase_ = goog.isDef(opt_ignoreCase) ?
<a name="line70"></a>        opt_ignoreCase : opt_uri.getIgnoreCase();
<a name="line71"></a>    this.setScheme(opt_uri.getScheme());
<a name="line72"></a>    this.setUserInfo(opt_uri.getUserInfo());
<a name="line73"></a>    this.setDomain(opt_uri.getDomain());
<a name="line74"></a>    this.setPort(opt_uri.getPort());
<a name="line75"></a>    this.setPath(opt_uri.getPath());
<a name="line76"></a>    this.setQueryData(opt_uri.getQueryData().clone());
<a name="line77"></a>    this.setFragment(opt_uri.getFragment());
<a name="line78"></a>  } else if (opt_uri &amp;&amp; (m = goog.uri.utils.split(String(opt_uri)))) {
<a name="line79"></a>    this.ignoreCase_ = !!opt_ignoreCase;
<a name="line80"></a>
<a name="line81"></a>    // Set the parts -- decoding as we do so.
<a name="line82"></a>    // COMPATABILITY NOTE - In IE, unmatched fields may be empty strings,
<a name="line83"></a>    // whereas in other browsers they will be undefined.
<a name="line84"></a>    this.setScheme(m[goog.uri.utils.ComponentIndex.SCHEME] || &#39;&#39;, true);
<a name="line85"></a>    this.setUserInfo(m[goog.uri.utils.ComponentIndex.USER_INFO] || &#39;&#39;, true);
<a name="line86"></a>    this.setDomain(m[goog.uri.utils.ComponentIndex.DOMAIN] || &#39;&#39;, true);
<a name="line87"></a>    this.setPort(m[goog.uri.utils.ComponentIndex.PORT]);
<a name="line88"></a>    this.setPath(m[goog.uri.utils.ComponentIndex.PATH] || &#39;&#39;, true);
<a name="line89"></a>    this.setQueryData(m[goog.uri.utils.ComponentIndex.QUERY_DATA] || &#39;&#39;, true);
<a name="line90"></a>    this.setFragment(m[goog.uri.utils.ComponentIndex.FRAGMENT] || &#39;&#39;, true);
<a name="line91"></a>
<a name="line92"></a>  } else {
<a name="line93"></a>    this.ignoreCase_ = !!opt_ignoreCase;
<a name="line94"></a>    this.queryData_ = new goog.Uri.QueryData(null, null, this.ignoreCase_);
<a name="line95"></a>  }
<a name="line96"></a>};
<a name="line97"></a>
<a name="line98"></a>
<a name="line99"></a>/**
<a name="line100"></a> * If true, we preserve the type of query parameters set programmatically.
<a name="line101"></a> *
<a name="line102"></a> * This means that if you set a parameter to a boolean, and then call
<a name="line103"></a> * getParameterValue, you will get a boolean back.
<a name="line104"></a> *
<a name="line105"></a> * If false, we will coerce parameters to strings, just as they would
<a name="line106"></a> * appear in real URIs.
<a name="line107"></a> *
<a name="line108"></a> * TODO(nicksantos): Remove this once people have time to fix all tests.
<a name="line109"></a> *
<a name="line110"></a> * @type {boolean}
<a name="line111"></a> */
<a name="line112"></a>goog.Uri.preserveParameterTypesCompatibilityFlag = false;
<a name="line113"></a>
<a name="line114"></a>
<a name="line115"></a>/**
<a name="line116"></a> * Parameter name added to stop caching.
<a name="line117"></a> * @type {string}
<a name="line118"></a> */
<a name="line119"></a>goog.Uri.RANDOM_PARAM = goog.uri.utils.StandardQueryParam.RANDOM;
<a name="line120"></a>
<a name="line121"></a>
<a name="line122"></a>/**
<a name="line123"></a> * Scheme such as &quot;http&quot;.
<a name="line124"></a> * @type {string}
<a name="line125"></a> * @private
<a name="line126"></a> */
<a name="line127"></a>goog.Uri.prototype.scheme_ = &#39;&#39;;
<a name="line128"></a>
<a name="line129"></a>
<a name="line130"></a>/**
<a name="line131"></a> * User credentials in the form &quot;username:password&quot;.
<a name="line132"></a> * @type {string}
<a name="line133"></a> * @private
<a name="line134"></a> */
<a name="line135"></a>goog.Uri.prototype.userInfo_ = &#39;&#39;;
<a name="line136"></a>
<a name="line137"></a>
<a name="line138"></a>/**
<a name="line139"></a> * Domain part, e.g. &quot;www.google.com&quot;.
<a name="line140"></a> * @type {string}
<a name="line141"></a> * @private
<a name="line142"></a> */
<a name="line143"></a>goog.Uri.prototype.domain_ = &#39;&#39;;
<a name="line144"></a>
<a name="line145"></a>
<a name="line146"></a>/**
<a name="line147"></a> * Port, e.g. 8080.
<a name="line148"></a> * @type {?number}
<a name="line149"></a> * @private
<a name="line150"></a> */
<a name="line151"></a>goog.Uri.prototype.port_ = null;
<a name="line152"></a>
<a name="line153"></a>
<a name="line154"></a>/**
<a name="line155"></a> * Path, e.g. &quot;/tests/img.png&quot;.
<a name="line156"></a> * @type {string}
<a name="line157"></a> * @private
<a name="line158"></a> */
<a name="line159"></a>goog.Uri.prototype.path_ = &#39;&#39;;
<a name="line160"></a>
<a name="line161"></a>
<a name="line162"></a>/**
<a name="line163"></a> * Object representing query data.
<a name="line164"></a> * @type {!goog.Uri.QueryData}
<a name="line165"></a> * @private
<a name="line166"></a> */
<a name="line167"></a>goog.Uri.prototype.queryData_;
<a name="line168"></a>
<a name="line169"></a>
<a name="line170"></a>/**
<a name="line171"></a> * The fragment without the #.
<a name="line172"></a> * @type {string}
<a name="line173"></a> * @private
<a name="line174"></a> */
<a name="line175"></a>goog.Uri.prototype.fragment_ = &#39;&#39;;
<a name="line176"></a>
<a name="line177"></a>
<a name="line178"></a>/**
<a name="line179"></a> * Whether or not this Uri should be treated as Read Only.
<a name="line180"></a> * @type {boolean}
<a name="line181"></a> * @private
<a name="line182"></a> */
<a name="line183"></a>goog.Uri.prototype.isReadOnly_ = false;
<a name="line184"></a>
<a name="line185"></a>
<a name="line186"></a>/**
<a name="line187"></a> * Whether or not to ignore case when comparing query params.
<a name="line188"></a> * @type {boolean}
<a name="line189"></a> * @private
<a name="line190"></a> */
<a name="line191"></a>goog.Uri.prototype.ignoreCase_ = false;
<a name="line192"></a>
<a name="line193"></a>
<a name="line194"></a>/**
<a name="line195"></a> * @return {string} The string form of the url.
<a name="line196"></a> * @override
<a name="line197"></a> */
<a name="line198"></a>goog.Uri.prototype.toString = function() {
<a name="line199"></a>  var out = [];
<a name="line200"></a>
<a name="line201"></a>  var scheme = this.getScheme();
<a name="line202"></a>  if (scheme) {
<a name="line203"></a>    out.push(goog.Uri.encodeSpecialChars_(
<a name="line204"></a>        scheme, goog.Uri.reDisallowedInSchemeOrUserInfo_), &#39;:&#39;);
<a name="line205"></a>  }
<a name="line206"></a>
<a name="line207"></a>  var domain = this.getDomain();
<a name="line208"></a>  if (domain) {
<a name="line209"></a>    out.push(&#39;//&#39;);
<a name="line210"></a>
<a name="line211"></a>    var userInfo = this.getUserInfo();
<a name="line212"></a>    if (userInfo) {
<a name="line213"></a>      out.push(goog.Uri.encodeSpecialChars_(
<a name="line214"></a>          userInfo, goog.Uri.reDisallowedInSchemeOrUserInfo_), &#39;@&#39;);
<a name="line215"></a>    }
<a name="line216"></a>
<a name="line217"></a>    out.push(goog.string.urlEncode(domain));
<a name="line218"></a>
<a name="line219"></a>    var port = this.getPort();
<a name="line220"></a>    if (port != null) {
<a name="line221"></a>      out.push(&#39;:&#39;, String(port));
<a name="line222"></a>    }
<a name="line223"></a>  }
<a name="line224"></a>
<a name="line225"></a>  var path = this.getPath();
<a name="line226"></a>  if (path) {
<a name="line227"></a>    if (this.hasDomain() &amp;&amp; path.charAt(0) != &#39;/&#39;) {
<a name="line228"></a>      out.push(&#39;/&#39;);
<a name="line229"></a>    }
<a name="line230"></a>    out.push(goog.Uri.encodeSpecialChars_(
<a name="line231"></a>        path,
<a name="line232"></a>        path.charAt(0) == &#39;/&#39; ?
<a name="line233"></a>            goog.Uri.reDisallowedInAbsolutePath_ :
<a name="line234"></a>            goog.Uri.reDisallowedInRelativePath_));
<a name="line235"></a>  }
<a name="line236"></a>
<a name="line237"></a>  var query = this.getEncodedQuery();
<a name="line238"></a>  if (query) {
<a name="line239"></a>    out.push(&#39;?&#39;, query);
<a name="line240"></a>  }
<a name="line241"></a>
<a name="line242"></a>  var fragment = this.getFragment();
<a name="line243"></a>  if (fragment) {
<a name="line244"></a>    out.push(&#39;#&#39;, goog.Uri.encodeSpecialChars_(
<a name="line245"></a>        fragment, goog.Uri.reDisallowedInFragment_));
<a name="line246"></a>  }
<a name="line247"></a>  return out.join(&#39;&#39;);
<a name="line248"></a>};
<a name="line249"></a>
<a name="line250"></a>
<a name="line251"></a>/**
<a name="line252"></a> * Resolves the given relative URI (a goog.Uri object), using the URI
<a name="line253"></a> * represented by this instance as the base URI.
<a name="line254"></a> *
<a name="line255"></a> * There are several kinds of relative URIs:&lt;br&gt;
<a name="line256"></a> * 1. foo - replaces the last part of the path, the whole query and fragment&lt;br&gt;
<a name="line257"></a> * 2. /foo - replaces the the path, the query and fragment&lt;br&gt;
<a name="line258"></a> * 3. //foo - replaces everything from the domain on.  foo is a domain name&lt;br&gt;
<a name="line259"></a> * 4. ?foo - replace the query and fragment&lt;br&gt;
<a name="line260"></a> * 5. #foo - replace the fragment only
<a name="line261"></a> *
<a name="line262"></a> * Additionally, if relative URI has a non-empty path, all &quot;..&quot; and &quot;.&quot;
<a name="line263"></a> * segments will be resolved, as described in RFC 3986.
<a name="line264"></a> *
<a name="line265"></a> * @param {goog.Uri} relativeUri The relative URI to resolve.
<a name="line266"></a> * @return {!goog.Uri} The resolved URI.
<a name="line267"></a> */
<a name="line268"></a>goog.Uri.prototype.resolve = function(relativeUri) {
<a name="line269"></a>
<a name="line270"></a>  var absoluteUri = this.clone();
<a name="line271"></a>
<a name="line272"></a>  // we satisfy these conditions by looking for the first part of relativeUri
<a name="line273"></a>  // that is not blank and applying defaults to the rest
<a name="line274"></a>
<a name="line275"></a>  var overridden = relativeUri.hasScheme();
<a name="line276"></a>
<a name="line277"></a>  if (overridden) {
<a name="line278"></a>    absoluteUri.setScheme(relativeUri.getScheme());
<a name="line279"></a>  } else {
<a name="line280"></a>    overridden = relativeUri.hasUserInfo();
<a name="line281"></a>  }
<a name="line282"></a>
<a name="line283"></a>  if (overridden) {
<a name="line284"></a>    absoluteUri.setUserInfo(relativeUri.getUserInfo());
<a name="line285"></a>  } else {
<a name="line286"></a>    overridden = relativeUri.hasDomain();
<a name="line287"></a>  }
<a name="line288"></a>
<a name="line289"></a>  if (overridden) {
<a name="line290"></a>    absoluteUri.setDomain(relativeUri.getDomain());
<a name="line291"></a>  } else {
<a name="line292"></a>    overridden = relativeUri.hasPort();
<a name="line293"></a>  }
<a name="line294"></a>
<a name="line295"></a>  var path = relativeUri.getPath();
<a name="line296"></a>  if (overridden) {
<a name="line297"></a>    absoluteUri.setPort(relativeUri.getPort());
<a name="line298"></a>  } else {
<a name="line299"></a>    overridden = relativeUri.hasPath();
<a name="line300"></a>    if (overridden) {
<a name="line301"></a>      // resolve path properly
<a name="line302"></a>      if (path.charAt(0) != &#39;/&#39;) {
<a name="line303"></a>        // path is relative
<a name="line304"></a>        if (this.hasDomain() &amp;&amp; !this.hasPath()) {
<a name="line305"></a>          // RFC 3986, section 5.2.3, case 1
<a name="line306"></a>          path = &#39;/&#39; + path;
<a name="line307"></a>        } else {
<a name="line308"></a>          // RFC 3986, section 5.2.3, case 2
<a name="line309"></a>          var lastSlashIndex = absoluteUri.getPath().lastIndexOf(&#39;/&#39;);
<a name="line310"></a>          if (lastSlashIndex != -1) {
<a name="line311"></a>            path = absoluteUri.getPath().substr(0, lastSlashIndex + 1) + path;
<a name="line312"></a>          }
<a name="line313"></a>        }
<a name="line314"></a>      }
<a name="line315"></a>      path = goog.Uri.removeDotSegments(path);
<a name="line316"></a>    }
<a name="line317"></a>  }
<a name="line318"></a>
<a name="line319"></a>  if (overridden) {
<a name="line320"></a>    absoluteUri.setPath(path);
<a name="line321"></a>  } else {
<a name="line322"></a>    overridden = relativeUri.hasQuery();
<a name="line323"></a>  }
<a name="line324"></a>
<a name="line325"></a>  if (overridden) {
<a name="line326"></a>    absoluteUri.setQueryData(relativeUri.getDecodedQuery());
<a name="line327"></a>  } else {
<a name="line328"></a>    overridden = relativeUri.hasFragment();
<a name="line329"></a>  }
<a name="line330"></a>
<a name="line331"></a>  if (overridden) {
<a name="line332"></a>    absoluteUri.setFragment(relativeUri.getFragment());
<a name="line333"></a>  }
<a name="line334"></a>
<a name="line335"></a>  return absoluteUri;
<a name="line336"></a>};
<a name="line337"></a>
<a name="line338"></a>
<a name="line339"></a>/**
<a name="line340"></a> * Clones the URI instance.
<a name="line341"></a> * @return {!goog.Uri} New instance of the URI objcet.
<a name="line342"></a> */
<a name="line343"></a>goog.Uri.prototype.clone = function() {
<a name="line344"></a>  return new goog.Uri(this);
<a name="line345"></a>};
<a name="line346"></a>
<a name="line347"></a>
<a name="line348"></a>/**
<a name="line349"></a> * @return {string} The encoded scheme/protocol for the URI.
<a name="line350"></a> */
<a name="line351"></a>goog.Uri.prototype.getScheme = function() {
<a name="line352"></a>  return this.scheme_;
<a name="line353"></a>};
<a name="line354"></a>
<a name="line355"></a>
<a name="line356"></a>/**
<a name="line357"></a> * Sets the scheme/protocol.
<a name="line358"></a> * @param {string} newScheme New scheme value.
<a name="line359"></a> * @param {boolean=} opt_decode Optional param for whether to decode new value.
<a name="line360"></a> * @return {!goog.Uri} Reference to this URI object.
<a name="line361"></a> */
<a name="line362"></a>goog.Uri.prototype.setScheme = function(newScheme, opt_decode) {
<a name="line363"></a>  this.enforceReadOnly();
<a name="line364"></a>  this.scheme_ = opt_decode ? goog.Uri.decodeOrEmpty_(newScheme) : newScheme;
<a name="line365"></a>
<a name="line366"></a>  // remove an : at the end of the scheme so somebody can pass in
<a name="line367"></a>  // window.location.protocol
<a name="line368"></a>  if (this.scheme_) {
<a name="line369"></a>    this.scheme_ = this.scheme_.replace(/:$/, &#39;&#39;);
<a name="line370"></a>  }
<a name="line371"></a>  return this;
<a name="line372"></a>};
<a name="line373"></a>
<a name="line374"></a>
<a name="line375"></a>/**
<a name="line376"></a> * @return {boolean} Whether the scheme has been set.
<a name="line377"></a> */
<a name="line378"></a>goog.Uri.prototype.hasScheme = function() {
<a name="line379"></a>  return !!this.scheme_;
<a name="line380"></a>};
<a name="line381"></a>
<a name="line382"></a>
<a name="line383"></a>/**
<a name="line384"></a> * @return {string} The decoded user info.
<a name="line385"></a> */
<a name="line386"></a>goog.Uri.prototype.getUserInfo = function() {
<a name="line387"></a>  return this.userInfo_;
<a name="line388"></a>};
<a name="line389"></a>
<a name="line390"></a>
<a name="line391"></a>/**
<a name="line392"></a> * Sets the userInfo.
<a name="line393"></a> * @param {string} newUserInfo New userInfo value.
<a name="line394"></a> * @param {boolean=} opt_decode Optional param for whether to decode new value.
<a name="line395"></a> * @return {!goog.Uri} Reference to this URI object.
<a name="line396"></a> */
<a name="line397"></a>goog.Uri.prototype.setUserInfo = function(newUserInfo, opt_decode) {
<a name="line398"></a>  this.enforceReadOnly();
<a name="line399"></a>  this.userInfo_ = opt_decode ? goog.Uri.decodeOrEmpty_(newUserInfo) :
<a name="line400"></a>                   newUserInfo;
<a name="line401"></a>  return this;
<a name="line402"></a>};
<a name="line403"></a>
<a name="line404"></a>
<a name="line405"></a>/**
<a name="line406"></a> * @return {boolean} Whether the user info has been set.
<a name="line407"></a> */
<a name="line408"></a>goog.Uri.prototype.hasUserInfo = function() {
<a name="line409"></a>  return !!this.userInfo_;
<a name="line410"></a>};
<a name="line411"></a>
<a name="line412"></a>
<a name="line413"></a>/**
<a name="line414"></a> * @return {string} The decoded domain.
<a name="line415"></a> */
<a name="line416"></a>goog.Uri.prototype.getDomain = function() {
<a name="line417"></a>  return this.domain_;
<a name="line418"></a>};
<a name="line419"></a>
<a name="line420"></a>
<a name="line421"></a>/**
<a name="line422"></a> * Sets the domain.
<a name="line423"></a> * @param {string} newDomain New domain value.
<a name="line424"></a> * @param {boolean=} opt_decode Optional param for whether to decode new value.
<a name="line425"></a> * @return {!goog.Uri} Reference to this URI object.
<a name="line426"></a> */
<a name="line427"></a>goog.Uri.prototype.setDomain = function(newDomain, opt_decode) {
<a name="line428"></a>  this.enforceReadOnly();
<a name="line429"></a>  this.domain_ = opt_decode ? goog.Uri.decodeOrEmpty_(newDomain) : newDomain;
<a name="line430"></a>  return this;
<a name="line431"></a>};
<a name="line432"></a>
<a name="line433"></a>
<a name="line434"></a>/**
<a name="line435"></a> * @return {boolean} Whether the domain has been set.
<a name="line436"></a> */
<a name="line437"></a>goog.Uri.prototype.hasDomain = function() {
<a name="line438"></a>  return !!this.domain_;
<a name="line439"></a>};
<a name="line440"></a>
<a name="line441"></a>
<a name="line442"></a>/**
<a name="line443"></a> * @return {?number} The port number.
<a name="line444"></a> */
<a name="line445"></a>goog.Uri.prototype.getPort = function() {
<a name="line446"></a>  return this.port_;
<a name="line447"></a>};
<a name="line448"></a>
<a name="line449"></a>
<a name="line450"></a>/**
<a name="line451"></a> * Sets the port number.
<a name="line452"></a> * @param {*} newPort Port number. Will be explicitly casted to a number.
<a name="line453"></a> * @return {!goog.Uri} Reference to this URI object.
<a name="line454"></a> */
<a name="line455"></a>goog.Uri.prototype.setPort = function(newPort) {
<a name="line456"></a>  this.enforceReadOnly();
<a name="line457"></a>
<a name="line458"></a>  if (newPort) {
<a name="line459"></a>    newPort = Number(newPort);
<a name="line460"></a>    if (isNaN(newPort) || newPort &lt; 0) {
<a name="line461"></a>      throw Error(&#39;Bad port number &#39; + newPort);
<a name="line462"></a>    }
<a name="line463"></a>    this.port_ = newPort;
<a name="line464"></a>  } else {
<a name="line465"></a>    this.port_ = null;
<a name="line466"></a>  }
<a name="line467"></a>
<a name="line468"></a>  return this;
<a name="line469"></a>};
<a name="line470"></a>
<a name="line471"></a>
<a name="line472"></a>/**
<a name="line473"></a> * @return {boolean} Whether the port has been set.
<a name="line474"></a> */
<a name="line475"></a>goog.Uri.prototype.hasPort = function() {
<a name="line476"></a>  return this.port_ != null;
<a name="line477"></a>};
<a name="line478"></a>
<a name="line479"></a>
<a name="line480"></a>/**
<a name="line481"></a>  * @return {string} The decoded path.
<a name="line482"></a> */
<a name="line483"></a>goog.Uri.prototype.getPath = function() {
<a name="line484"></a>  return this.path_;
<a name="line485"></a>};
<a name="line486"></a>
<a name="line487"></a>
<a name="line488"></a>/**
<a name="line489"></a> * Sets the path.
<a name="line490"></a> * @param {string} newPath New path value.
<a name="line491"></a> * @param {boolean=} opt_decode Optional param for whether to decode new value.
<a name="line492"></a> * @return {!goog.Uri} Reference to this URI object.
<a name="line493"></a> */
<a name="line494"></a>goog.Uri.prototype.setPath = function(newPath, opt_decode) {
<a name="line495"></a>  this.enforceReadOnly();
<a name="line496"></a>  this.path_ = opt_decode ? goog.Uri.decodeOrEmpty_(newPath) : newPath;
<a name="line497"></a>  return this;
<a name="line498"></a>};
<a name="line499"></a>
<a name="line500"></a>
<a name="line501"></a>/**
<a name="line502"></a> * @return {boolean} Whether the path has been set.
<a name="line503"></a> */
<a name="line504"></a>goog.Uri.prototype.hasPath = function() {
<a name="line505"></a>  return !!this.path_;
<a name="line506"></a>};
<a name="line507"></a>
<a name="line508"></a>
<a name="line509"></a>/**
<a name="line510"></a> * @return {boolean} Whether the query string has been set.
<a name="line511"></a> */
<a name="line512"></a>goog.Uri.prototype.hasQuery = function() {
<a name="line513"></a>  return this.queryData_.toString() !== &#39;&#39;;
<a name="line514"></a>};
<a name="line515"></a>
<a name="line516"></a>
<a name="line517"></a>/**
<a name="line518"></a> * Sets the query data.
<a name="line519"></a> * @param {goog.Uri.QueryData|string|undefined} queryData QueryData object.
<a name="line520"></a> * @param {boolean=} opt_decode Optional param for whether to decode new value.
<a name="line521"></a> *     Applies only if queryData is a string.
<a name="line522"></a> * @return {!goog.Uri} Reference to this URI object.
<a name="line523"></a> */
<a name="line524"></a>goog.Uri.prototype.setQueryData = function(queryData, opt_decode) {
<a name="line525"></a>  this.enforceReadOnly();
<a name="line526"></a>
<a name="line527"></a>  if (queryData instanceof goog.Uri.QueryData) {
<a name="line528"></a>    this.queryData_ = queryData;
<a name="line529"></a>    this.queryData_.setIgnoreCase(this.ignoreCase_);
<a name="line530"></a>  } else {
<a name="line531"></a>    if (!opt_decode) {
<a name="line532"></a>      // QueryData accepts encoded query string, so encode it if
<a name="line533"></a>      // opt_decode flag is not true.
<a name="line534"></a>      queryData = goog.Uri.encodeSpecialChars_(queryData,
<a name="line535"></a>                                               goog.Uri.reDisallowedInQuery_);
<a name="line536"></a>    }
<a name="line537"></a>    this.queryData_ = new goog.Uri.QueryData(queryData, null, this.ignoreCase_);
<a name="line538"></a>  }
<a name="line539"></a>
<a name="line540"></a>  return this;
<a name="line541"></a>};
<a name="line542"></a>
<a name="line543"></a>
<a name="line544"></a>/**
<a name="line545"></a> * Sets the URI query.
<a name="line546"></a> * @param {string} newQuery New query value.
<a name="line547"></a> * @param {boolean=} opt_decode Optional param for whether to decode new value.
<a name="line548"></a> * @return {!goog.Uri} Reference to this URI object.
<a name="line549"></a> */
<a name="line550"></a>goog.Uri.prototype.setQuery = function(newQuery, opt_decode) {
<a name="line551"></a>  return this.setQueryData(newQuery, opt_decode);
<a name="line552"></a>};
<a name="line553"></a>
<a name="line554"></a>
<a name="line555"></a>/**
<a name="line556"></a> * @return {string} The encoded URI query, not including the ?.
<a name="line557"></a> */
<a name="line558"></a>goog.Uri.prototype.getEncodedQuery = function() {
<a name="line559"></a>  return this.queryData_.toString();
<a name="line560"></a>};
<a name="line561"></a>
<a name="line562"></a>
<a name="line563"></a>/**
<a name="line564"></a> * @return {string} The decoded URI query, not including the ?.
<a name="line565"></a> */
<a name="line566"></a>goog.Uri.prototype.getDecodedQuery = function() {
<a name="line567"></a>  return this.queryData_.toDecodedString();
<a name="line568"></a>};
<a name="line569"></a>
<a name="line570"></a>
<a name="line571"></a>/**
<a name="line572"></a> * Returns the query data.
<a name="line573"></a> * @return {goog.Uri.QueryData} QueryData object.
<a name="line574"></a> */
<a name="line575"></a>goog.Uri.prototype.getQueryData = function() {
<a name="line576"></a>  return this.queryData_;
<a name="line577"></a>};
<a name="line578"></a>
<a name="line579"></a>
<a name="line580"></a>/**
<a name="line581"></a> * @return {string} The encoded URI query, not including the ?.
<a name="line582"></a> *
<a name="line583"></a> * Warning: This method, unlike other getter methods, returns encoded
<a name="line584"></a> * value, instead of decoded one.
<a name="line585"></a> */
<a name="line586"></a>goog.Uri.prototype.getQuery = function() {
<a name="line587"></a>  return this.getEncodedQuery();
<a name="line588"></a>};
<a name="line589"></a>
<a name="line590"></a>
<a name="line591"></a>/**
<a name="line592"></a> * Sets the value of the named query parameters, clearing previous values for
<a name="line593"></a> * that key.
<a name="line594"></a> *
<a name="line595"></a> * @param {string} key The parameter to set.
<a name="line596"></a> * @param {*} value The new value.
<a name="line597"></a> * @return {!goog.Uri} Reference to this URI object.
<a name="line598"></a> */
<a name="line599"></a>goog.Uri.prototype.setParameterValue = function(key, value) {
<a name="line600"></a>  this.enforceReadOnly();
<a name="line601"></a>  this.queryData_.set(key, value);
<a name="line602"></a>  return this;
<a name="line603"></a>};
<a name="line604"></a>
<a name="line605"></a>
<a name="line606"></a>/**
<a name="line607"></a> * Sets the values of the named query parameters, clearing previous values for
<a name="line608"></a> * that key.  Not new values will currently be moved to the end of the query
<a name="line609"></a> * string.
<a name="line610"></a> *
<a name="line611"></a> * So, &lt;code&gt;goog.Uri.parse(&#39;foo?a=b&amp;c=d&amp;e=f&#39;).setParameterValues(&#39;c&#39;, [&#39;new&#39;])
<a name="line612"></a> * &lt;/code&gt; yields &lt;tt&gt;foo?a=b&amp;e=f&amp;c=new&lt;/tt&gt;.&lt;/p&gt;
<a name="line613"></a> *
<a name="line614"></a> * @param {string} key The parameter to set.
<a name="line615"></a> * @param {*} values The new values. If values is a single
<a name="line616"></a> *     string then it will be treated as the sole value.
<a name="line617"></a> * @return {!goog.Uri} Reference to this URI object.
<a name="line618"></a> */
<a name="line619"></a>goog.Uri.prototype.setParameterValues = function(key, values) {
<a name="line620"></a>  this.enforceReadOnly();
<a name="line621"></a>
<a name="line622"></a>  if (!goog.isArray(values)) {
<a name="line623"></a>    values = [String(values)];
<a name="line624"></a>  }
<a name="line625"></a>
<a name="line626"></a>  // TODO(nicksantos): This cast shouldn&#39;t be necessary.
<a name="line627"></a>  this.queryData_.setValues(key, /** @type {Array} */ (values));
<a name="line628"></a>
<a name="line629"></a>  return this;
<a name="line630"></a>};
<a name="line631"></a>
<a name="line632"></a>
<a name="line633"></a>/**
<a name="line634"></a> * Returns the value&lt;b&gt;s&lt;/b&gt; for a given cgi parameter as a list of decoded
<a name="line635"></a> * query parameter values.
<a name="line636"></a> * @param {string} name The parameter to get values for.
<a name="line637"></a> * @return {Array} The values for a given cgi parameter as a list of
<a name="line638"></a> *     decoded query parameter values.
<a name="line639"></a> */
<a name="line640"></a>goog.Uri.prototype.getParameterValues = function(name) {
<a name="line641"></a>  return this.queryData_.getValues(name);
<a name="line642"></a>};
<a name="line643"></a>
<a name="line644"></a>
<a name="line645"></a>/**
<a name="line646"></a> * Returns the first value for a given cgi parameter or undefined if the given
<a name="line647"></a> * parameter name does not appear in the query string.
<a name="line648"></a> * @param {string} paramName Unescaped parameter name.
<a name="line649"></a> * @return {string|undefined} The first value for a given cgi parameter or
<a name="line650"></a> *     undefined if the given parameter name does not appear in the query
<a name="line651"></a> *     string.
<a name="line652"></a> */
<a name="line653"></a>goog.Uri.prototype.getParameterValue = function(paramName) {
<a name="line654"></a>  // NOTE(nicksantos): This type-cast is a lie when
<a name="line655"></a>  // preserveParameterTypesCompatibilityFlag is set to true.
<a name="line656"></a>  // But this should only be set to true in tests.
<a name="line657"></a>  return /** @type {string|undefined} */ (this.queryData_.get(paramName));
<a name="line658"></a>};
<a name="line659"></a>
<a name="line660"></a>
<a name="line661"></a>/**
<a name="line662"></a> * @return {string} The URI fragment, not including the #.
<a name="line663"></a> */
<a name="line664"></a>goog.Uri.prototype.getFragment = function() {
<a name="line665"></a>  return this.fragment_;
<a name="line666"></a>};
<a name="line667"></a>
<a name="line668"></a>
<a name="line669"></a>/**
<a name="line670"></a> * Sets the URI fragment.
<a name="line671"></a> * @param {string} newFragment New fragment value.
<a name="line672"></a> * @param {boolean=} opt_decode Optional param for whether to decode new value.
<a name="line673"></a> * @return {!goog.Uri} Reference to this URI object.
<a name="line674"></a> */
<a name="line675"></a>goog.Uri.prototype.setFragment = function(newFragment, opt_decode) {
<a name="line676"></a>  this.enforceReadOnly();
<a name="line677"></a>  this.fragment_ = opt_decode ? goog.Uri.decodeOrEmpty_(newFragment) :
<a name="line678"></a>                   newFragment;
<a name="line679"></a>  return this;
<a name="line680"></a>};
<a name="line681"></a>
<a name="line682"></a>
<a name="line683"></a>/**
<a name="line684"></a> * @return {boolean} Whether the URI has a fragment set.
<a name="line685"></a> */
<a name="line686"></a>goog.Uri.prototype.hasFragment = function() {
<a name="line687"></a>  return !!this.fragment_;
<a name="line688"></a>};
<a name="line689"></a>
<a name="line690"></a>
<a name="line691"></a>/**
<a name="line692"></a> * Returns true if this has the same domain as that of uri2.
<a name="line693"></a> * @param {goog.Uri} uri2 The URI object to compare to.
<a name="line694"></a> * @return {boolean} true if same domain; false otherwise.
<a name="line695"></a> */
<a name="line696"></a>goog.Uri.prototype.hasSameDomainAs = function(uri2) {
<a name="line697"></a>  return ((!this.hasDomain() &amp;&amp; !uri2.hasDomain()) ||
<a name="line698"></a>          this.getDomain() == uri2.getDomain()) &amp;&amp;
<a name="line699"></a>      ((!this.hasPort() &amp;&amp; !uri2.hasPort()) ||
<a name="line700"></a>          this.getPort() == uri2.getPort());
<a name="line701"></a>};
<a name="line702"></a>
<a name="line703"></a>
<a name="line704"></a>/**
<a name="line705"></a> * Adds a random parameter to the Uri.
<a name="line706"></a> * @return {!goog.Uri} Reference to this Uri object.
<a name="line707"></a> */
<a name="line708"></a>goog.Uri.prototype.makeUnique = function() {
<a name="line709"></a>  this.enforceReadOnly();
<a name="line710"></a>  this.setParameterValue(goog.Uri.RANDOM_PARAM, goog.string.getRandomString());
<a name="line711"></a>
<a name="line712"></a>  return this;
<a name="line713"></a>};
<a name="line714"></a>
<a name="line715"></a>
<a name="line716"></a>/**
<a name="line717"></a> * Removes the named query parameter.
<a name="line718"></a> *
<a name="line719"></a> * @param {string} key The parameter to remove.
<a name="line720"></a> * @return {!goog.Uri} Reference to this URI object.
<a name="line721"></a> */
<a name="line722"></a>goog.Uri.prototype.removeParameter = function(key) {
<a name="line723"></a>  this.enforceReadOnly();
<a name="line724"></a>  this.queryData_.remove(key);
<a name="line725"></a>  return this;
<a name="line726"></a>};
<a name="line727"></a>
<a name="line728"></a>
<a name="line729"></a>/**
<a name="line730"></a> * Sets whether Uri is read only. If this goog.Uri is read-only,
<a name="line731"></a> * enforceReadOnly_ will be called at the start of any function that may modify
<a name="line732"></a> * this Uri.
<a name="line733"></a> * @param {boolean} isReadOnly whether this goog.Uri should be read only.
<a name="line734"></a> * @return {!goog.Uri} Reference to this Uri object.
<a name="line735"></a> */
<a name="line736"></a>goog.Uri.prototype.setReadOnly = function(isReadOnly) {
<a name="line737"></a>  this.isReadOnly_ = isReadOnly;
<a name="line738"></a>  return this;
<a name="line739"></a>};
<a name="line740"></a>
<a name="line741"></a>
<a name="line742"></a>/**
<a name="line743"></a> * @return {boolean} Whether the URI is read only.
<a name="line744"></a> */
<a name="line745"></a>goog.Uri.prototype.isReadOnly = function() {
<a name="line746"></a>  return this.isReadOnly_;
<a name="line747"></a>};
<a name="line748"></a>
<a name="line749"></a>
<a name="line750"></a>/**
<a name="line751"></a> * Checks if this Uri has been marked as read only, and if so, throws an error.
<a name="line752"></a> * This should be called whenever any modifying function is called.
<a name="line753"></a> */
<a name="line754"></a>goog.Uri.prototype.enforceReadOnly = function() {
<a name="line755"></a>  if (this.isReadOnly_) {
<a name="line756"></a>    throw Error(&#39;Tried to modify a read-only Uri&#39;);
<a name="line757"></a>  }
<a name="line758"></a>};
<a name="line759"></a>
<a name="line760"></a>
<a name="line761"></a>/**
<a name="line762"></a> * Sets whether to ignore case.
<a name="line763"></a> * NOTE: If there are already key/value pairs in the QueryData, and
<a name="line764"></a> * ignoreCase_ is set to false, the keys will all be lower-cased.
<a name="line765"></a> * @param {boolean} ignoreCase whether this goog.Uri should ignore case.
<a name="line766"></a> * @return {!goog.Uri} Reference to this Uri object.
<a name="line767"></a> */
<a name="line768"></a>goog.Uri.prototype.setIgnoreCase = function(ignoreCase) {
<a name="line769"></a>  this.ignoreCase_ = ignoreCase;
<a name="line770"></a>  if (this.queryData_) {
<a name="line771"></a>    this.queryData_.setIgnoreCase(ignoreCase);
<a name="line772"></a>  }
<a name="line773"></a>  return this;
<a name="line774"></a>};
<a name="line775"></a>
<a name="line776"></a>
<a name="line777"></a>/**
<a name="line778"></a> * @return {boolean} Whether to ignore case.
<a name="line779"></a> */
<a name="line780"></a>goog.Uri.prototype.getIgnoreCase = function() {
<a name="line781"></a>  return this.ignoreCase_;
<a name="line782"></a>};
<a name="line783"></a>
<a name="line784"></a>
<a name="line785"></a>//==============================================================================
<a name="line786"></a>// Static members
<a name="line787"></a>//==============================================================================
<a name="line788"></a>
<a name="line789"></a>
<a name="line790"></a>/**
<a name="line791"></a> * Creates a uri from the string form.  Basically an alias of new goog.Uri().
<a name="line792"></a> * If a Uri object is passed to parse then it will return a clone of the object.
<a name="line793"></a> *
<a name="line794"></a> * @param {*} uri Raw URI string or instance of Uri
<a name="line795"></a> *     object.
<a name="line796"></a> * @param {boolean=} opt_ignoreCase Whether to ignore the case of parameter
<a name="line797"></a> * names in #getParameterValue.
<a name="line798"></a> * @return {!goog.Uri} The new URI object.
<a name="line799"></a> */
<a name="line800"></a>goog.Uri.parse = function(uri, opt_ignoreCase) {
<a name="line801"></a>  return uri instanceof goog.Uri ?
<a name="line802"></a>         uri.clone() : new goog.Uri(uri, opt_ignoreCase);
<a name="line803"></a>};
<a name="line804"></a>
<a name="line805"></a>
<a name="line806"></a>/**
<a name="line807"></a> * Creates a new goog.Uri object from unencoded parts.
<a name="line808"></a> *
<a name="line809"></a> * @param {?string=} opt_scheme Scheme/protocol or full URI to parse.
<a name="line810"></a> * @param {?string=} opt_userInfo username:password.
<a name="line811"></a> * @param {?string=} opt_domain www.google.com.
<a name="line812"></a> * @param {?number=} opt_port 9830.
<a name="line813"></a> * @param {?string=} opt_path /some/path/to/a/file.html.
<a name="line814"></a> * @param {string|goog.Uri.QueryData=} opt_query a=1&amp;b=2.
<a name="line815"></a> * @param {?string=} opt_fragment The fragment without the #.
<a name="line816"></a> * @param {boolean=} opt_ignoreCase Whether to ignore parameter name case in
<a name="line817"></a> *     #getParameterValue.
<a name="line818"></a> *
<a name="line819"></a> * @return {!goog.Uri} The new URI object.
<a name="line820"></a> */
<a name="line821"></a>goog.Uri.create = function(opt_scheme, opt_userInfo, opt_domain, opt_port,
<a name="line822"></a>                           opt_path, opt_query, opt_fragment, opt_ignoreCase) {
<a name="line823"></a>
<a name="line824"></a>  var uri = new goog.Uri(null, opt_ignoreCase);
<a name="line825"></a>
<a name="line826"></a>  // Only set the parts if they are defined and not empty strings.
<a name="line827"></a>  opt_scheme &amp;&amp; uri.setScheme(opt_scheme);
<a name="line828"></a>  opt_userInfo &amp;&amp; uri.setUserInfo(opt_userInfo);
<a name="line829"></a>  opt_domain &amp;&amp; uri.setDomain(opt_domain);
<a name="line830"></a>  opt_port &amp;&amp; uri.setPort(opt_port);
<a name="line831"></a>  opt_path &amp;&amp; uri.setPath(opt_path);
<a name="line832"></a>  opt_query &amp;&amp; uri.setQueryData(opt_query);
<a name="line833"></a>  opt_fragment &amp;&amp; uri.setFragment(opt_fragment);
<a name="line834"></a>
<a name="line835"></a>  return uri;
<a name="line836"></a>};
<a name="line837"></a>
<a name="line838"></a>
<a name="line839"></a>/**
<a name="line840"></a> * Resolves a relative Uri against a base Uri, accepting both strings and
<a name="line841"></a> * Uri objects.
<a name="line842"></a> *
<a name="line843"></a> * @param {*} base Base Uri.
<a name="line844"></a> * @param {*} rel Relative Uri.
<a name="line845"></a> * @return {!goog.Uri} Resolved uri.
<a name="line846"></a> */
<a name="line847"></a>goog.Uri.resolve = function(base, rel) {
<a name="line848"></a>  if (!(base instanceof goog.Uri)) {
<a name="line849"></a>    base = goog.Uri.parse(base);
<a name="line850"></a>  }
<a name="line851"></a>
<a name="line852"></a>  if (!(rel instanceof goog.Uri)) {
<a name="line853"></a>    rel = goog.Uri.parse(rel);
<a name="line854"></a>  }
<a name="line855"></a>
<a name="line856"></a>  return base.resolve(rel);
<a name="line857"></a>};
<a name="line858"></a>
<a name="line859"></a>
<a name="line860"></a>/**
<a name="line861"></a> * Removes dot segments in given path component, as described in
<a name="line862"></a> * RFC 3986, section 5.2.4.
<a name="line863"></a> *
<a name="line864"></a> * @param {string} path A non-empty path component.
<a name="line865"></a> * @return {string} Path component with removed dot segments.
<a name="line866"></a> */
<a name="line867"></a>goog.Uri.removeDotSegments = function(path) {
<a name="line868"></a>  if (path == &#39;..&#39; || path == &#39;.&#39;) {
<a name="line869"></a>    return &#39;&#39;;
<a name="line870"></a>
<a name="line871"></a>  } else if (!goog.string.contains(path, &#39;./&#39;) &amp;&amp;
<a name="line872"></a>             !goog.string.contains(path, &#39;/.&#39;)) {
<a name="line873"></a>    // This optimization detects uris which do not contain dot-segments,
<a name="line874"></a>    // and as a consequence do not require any processing.
<a name="line875"></a>    return path;
<a name="line876"></a>
<a name="line877"></a>  } else {
<a name="line878"></a>    var leadingSlash = goog.string.startsWith(path, &#39;/&#39;);
<a name="line879"></a>    var segments = path.split(&#39;/&#39;);
<a name="line880"></a>    var out = [];
<a name="line881"></a>
<a name="line882"></a>    for (var pos = 0; pos &lt; segments.length; ) {
<a name="line883"></a>      var segment = segments[pos++];
<a name="line884"></a>
<a name="line885"></a>      if (segment == &#39;.&#39;) {
<a name="line886"></a>        if (leadingSlash &amp;&amp; pos == segments.length) {
<a name="line887"></a>          out.push(&#39;&#39;);
<a name="line888"></a>        }
<a name="line889"></a>      } else if (segment == &#39;..&#39;) {
<a name="line890"></a>        if (out.length &gt; 1 || out.length == 1 &amp;&amp; out[0] != &#39;&#39;) {
<a name="line891"></a>          out.pop();
<a name="line892"></a>        }
<a name="line893"></a>        if (leadingSlash &amp;&amp; pos == segments.length) {
<a name="line894"></a>          out.push(&#39;&#39;);
<a name="line895"></a>        }
<a name="line896"></a>      } else {
<a name="line897"></a>        out.push(segment);
<a name="line898"></a>        leadingSlash = true;
<a name="line899"></a>      }
<a name="line900"></a>    }
<a name="line901"></a>
<a name="line902"></a>    return out.join(&#39;/&#39;);
<a name="line903"></a>  }
<a name="line904"></a>};
<a name="line905"></a>
<a name="line906"></a>
<a name="line907"></a>/**
<a name="line908"></a> * Decodes a value or returns the empty string if it isn&#39;t defined or empty.
<a name="line909"></a> * @param {string|undefined} val Value to decode.
<a name="line910"></a> * @return {string} Decoded value.
<a name="line911"></a> * @private
<a name="line912"></a> */
<a name="line913"></a>goog.Uri.decodeOrEmpty_ = function(val) {
<a name="line914"></a>  // Don&#39;t use UrlDecode() here because val is not a query parameter.
<a name="line915"></a>  return val ? decodeURIComponent(val) : &#39;&#39;;
<a name="line916"></a>};
<a name="line917"></a>
<a name="line918"></a>
<a name="line919"></a>/**
<a name="line920"></a> * If unescapedPart is non null, then escapes any characters in it that aren&#39;t
<a name="line921"></a> * valid characters in a url and also escapes any special characters that
<a name="line922"></a> * appear in extra.
<a name="line923"></a> *
<a name="line924"></a> * @param {*} unescapedPart The string to encode.
<a name="line925"></a> * @param {RegExp} extra A character set of characters in [\01-\177].
<a name="line926"></a> * @return {?string} null iff unescapedPart == null.
<a name="line927"></a> * @private
<a name="line928"></a> */
<a name="line929"></a>goog.Uri.encodeSpecialChars_ = function(unescapedPart, extra) {
<a name="line930"></a>  if (goog.isString(unescapedPart)) {
<a name="line931"></a>    return encodeURI(unescapedPart).replace(extra, goog.Uri.encodeChar_);
<a name="line932"></a>  }
<a name="line933"></a>  return null;
<a name="line934"></a>};
<a name="line935"></a>
<a name="line936"></a>
<a name="line937"></a>/**
<a name="line938"></a> * Converts a character in [\01-\177] to its unicode character equivalent.
<a name="line939"></a> * @param {string} ch One character string.
<a name="line940"></a> * @return {string} Encoded string.
<a name="line941"></a> * @private
<a name="line942"></a> */
<a name="line943"></a>goog.Uri.encodeChar_ = function(ch) {
<a name="line944"></a>  var n = ch.charCodeAt(0);
<a name="line945"></a>  return &#39;%&#39; + ((n &gt;&gt; 4) &amp; 0xf).toString(16) + (n &amp; 0xf).toString(16);
<a name="line946"></a>};
<a name="line947"></a>
<a name="line948"></a>
<a name="line949"></a>/**
<a name="line950"></a> * Regular expression for characters that are disallowed in the scheme or
<a name="line951"></a> * userInfo part of the URI.
<a name="line952"></a> * @type {RegExp}
<a name="line953"></a> * @private
<a name="line954"></a> */
<a name="line955"></a>goog.Uri.reDisallowedInSchemeOrUserInfo_ = /[#\/\?@]/g;
<a name="line956"></a>
<a name="line957"></a>
<a name="line958"></a>/**
<a name="line959"></a> * Regular expression for characters that are disallowed in a relative path.
<a name="line960"></a> * @type {RegExp}
<a name="line961"></a> * @private
<a name="line962"></a> */
<a name="line963"></a>goog.Uri.reDisallowedInRelativePath_ = /[\#\?:]/g;
<a name="line964"></a>
<a name="line965"></a>
<a name="line966"></a>/**
<a name="line967"></a> * Regular expression for characters that are disallowed in an absolute path.
<a name="line968"></a> * @type {RegExp}
<a name="line969"></a> * @private
<a name="line970"></a> */
<a name="line971"></a>goog.Uri.reDisallowedInAbsolutePath_ = /[\#\?]/g;
<a name="line972"></a>
<a name="line973"></a>
<a name="line974"></a>/**
<a name="line975"></a> * Regular expression for characters that are disallowed in the query.
<a name="line976"></a> * @type {RegExp}
<a name="line977"></a> * @private
<a name="line978"></a> */
<a name="line979"></a>goog.Uri.reDisallowedInQuery_ = /[\#\?@]/g;
<a name="line980"></a>
<a name="line981"></a>
<a name="line982"></a>/**
<a name="line983"></a> * Regular expression for characters that are disallowed in the fragment.
<a name="line984"></a> * @type {RegExp}
<a name="line985"></a> * @private
<a name="line986"></a> */
<a name="line987"></a>goog.Uri.reDisallowedInFragment_ = /#/g;
<a name="line988"></a>
<a name="line989"></a>
<a name="line990"></a>/**
<a name="line991"></a> * Checks whether two URIs have the same domain.
<a name="line992"></a> * @param {string} uri1String First URI string.
<a name="line993"></a> * @param {string} uri2String Second URI string.
<a name="line994"></a> * @return {boolean} true if the two URIs have the same domain; false otherwise.
<a name="line995"></a> */
<a name="line996"></a>goog.Uri.haveSameDomain = function(uri1String, uri2String) {
<a name="line997"></a>  // Differs from goog.uri.utils.haveSameDomain, since this ignores scheme.
<a name="line998"></a>  // TODO(gboyer): Have this just call goog.uri.util.haveSameDomain.
<a name="line999"></a>  var pieces1 = goog.uri.utils.split(uri1String);
<a name="line1000"></a>  var pieces2 = goog.uri.utils.split(uri2String);
<a name="line1001"></a>  return pieces1[goog.uri.utils.ComponentIndex.DOMAIN] ==
<a name="line1002"></a>             pieces2[goog.uri.utils.ComponentIndex.DOMAIN] &amp;&amp;
<a name="line1003"></a>         pieces1[goog.uri.utils.ComponentIndex.PORT] ==
<a name="line1004"></a>             pieces2[goog.uri.utils.ComponentIndex.PORT];
<a name="line1005"></a>};
<a name="line1006"></a>
<a name="line1007"></a>
<a name="line1008"></a>
<a name="line1009"></a>/**
<a name="line1010"></a> * Class used to represent URI query parameters.  It is essentially a hash of
<a name="line1011"></a> * name-value pairs, though a name can be present more than once.
<a name="line1012"></a> *
<a name="line1013"></a> * Has the same interface as the collections in goog.structs.
<a name="line1014"></a> *
<a name="line1015"></a> * @param {?string=} opt_query Optional encoded query string to parse into
<a name="line1016"></a> *     the object.
<a name="line1017"></a> * @param {goog.Uri=} opt_uri Optional uri object that should have its
<a name="line1018"></a> *     cache invalidated when this object updates. Deprecated -- this
<a name="line1019"></a> *     is no longer required.
<a name="line1020"></a> * @param {boolean=} opt_ignoreCase If true, ignore the case of the parameter
<a name="line1021"></a> *     name in #get.
<a name="line1022"></a> * @constructor
<a name="line1023"></a> */
<a name="line1024"></a>goog.Uri.QueryData = function(opt_query, opt_uri, opt_ignoreCase) {
<a name="line1025"></a>  /**
<a name="line1026"></a>   * Encoded query string, or null if it requires computing from the key map.
<a name="line1027"></a>   * @type {?string}
<a name="line1028"></a>   * @private
<a name="line1029"></a>   */
<a name="line1030"></a>  this.encodedQuery_ = opt_query || null;
<a name="line1031"></a>
<a name="line1032"></a>  /**
<a name="line1033"></a>   * If true, ignore the case of the parameter name in #get.
<a name="line1034"></a>   * @type {boolean}
<a name="line1035"></a>   * @private
<a name="line1036"></a>   */
<a name="line1037"></a>  this.ignoreCase_ = !!opt_ignoreCase;
<a name="line1038"></a>};
<a name="line1039"></a>
<a name="line1040"></a>
<a name="line1041"></a>/**
<a name="line1042"></a> * If the underlying key map is not yet initialized, it parses the
<a name="line1043"></a> * query string and fills the map with parsed data.
<a name="line1044"></a> * @private
<a name="line1045"></a> */
<a name="line1046"></a>goog.Uri.QueryData.prototype.ensureKeyMapInitialized_ = function() {
<a name="line1047"></a>  if (!this.keyMap_) {
<a name="line1048"></a>    this.keyMap_ = new goog.structs.Map();
<a name="line1049"></a>    this.count_ = 0;
<a name="line1050"></a>
<a name="line1051"></a>    if (this.encodedQuery_) {
<a name="line1052"></a>      var pairs = this.encodedQuery_.split(&#39;&amp;&#39;);
<a name="line1053"></a>      for (var i = 0; i &lt; pairs.length; i++) {
<a name="line1054"></a>        var indexOfEquals = pairs[i].indexOf(&#39;=&#39;);
<a name="line1055"></a>        var name = null;
<a name="line1056"></a>        var value = null;
<a name="line1057"></a>        if (indexOfEquals &gt;= 0) {
<a name="line1058"></a>          name = pairs[i].substring(0, indexOfEquals);
<a name="line1059"></a>          value = pairs[i].substring(indexOfEquals + 1);
<a name="line1060"></a>        } else {
<a name="line1061"></a>          name = pairs[i];
<a name="line1062"></a>        }
<a name="line1063"></a>        name = goog.string.urlDecode(name);
<a name="line1064"></a>        name = this.getKeyName_(name);
<a name="line1065"></a>        this.add(name, value ? goog.string.urlDecode(value) : &#39;&#39;);
<a name="line1066"></a>      }
<a name="line1067"></a>    }
<a name="line1068"></a>  }
<a name="line1069"></a>};
<a name="line1070"></a>
<a name="line1071"></a>
<a name="line1072"></a>/**
<a name="line1073"></a> * Creates a new query data instance from a map of names and values.
<a name="line1074"></a> *
<a name="line1075"></a> * @param {!goog.structs.Map|!Object} map Map of string parameter
<a name="line1076"></a> *     names to parameter value. If parameter value is an array, it is
<a name="line1077"></a> *     treated as if the key maps to each individual value in the
<a name="line1078"></a> *     array.
<a name="line1079"></a> * @param {goog.Uri=} opt_uri URI object that should have its cache
<a name="line1080"></a> *     invalidated when this object updates.
<a name="line1081"></a> * @param {boolean=} opt_ignoreCase If true, ignore the case of the parameter
<a name="line1082"></a> *     name in #get.
<a name="line1083"></a> * @return {!goog.Uri.QueryData} The populated query data instance.
<a name="line1084"></a> */
<a name="line1085"></a>goog.Uri.QueryData.createFromMap = function(map, opt_uri, opt_ignoreCase) {
<a name="line1086"></a>  var keys = goog.structs.getKeys(map);
<a name="line1087"></a>  if (typeof keys == &#39;undefined&#39;) {
<a name="line1088"></a>    throw Error(&#39;Keys are undefined&#39;);
<a name="line1089"></a>  }
<a name="line1090"></a>
<a name="line1091"></a>  var queryData = new goog.Uri.QueryData(null, null, opt_ignoreCase);
<a name="line1092"></a>  var values = goog.structs.getValues(map);
<a name="line1093"></a>  for (var i = 0; i &lt; keys.length; i++) {
<a name="line1094"></a>    var key = keys[i];
<a name="line1095"></a>    var value = values[i];
<a name="line1096"></a>    if (!goog.isArray(value)) {
<a name="line1097"></a>      queryData.add(key, value);
<a name="line1098"></a>    } else {
<a name="line1099"></a>      queryData.setValues(key, value);
<a name="line1100"></a>    }
<a name="line1101"></a>  }
<a name="line1102"></a>  return queryData;
<a name="line1103"></a>};
<a name="line1104"></a>
<a name="line1105"></a>
<a name="line1106"></a>/**
<a name="line1107"></a> * Creates a new query data instance from parallel arrays of parameter names
<a name="line1108"></a> * and values. Allows for duplicate parameter names. Throws an error if the
<a name="line1109"></a> * lengths of the arrays differ.
<a name="line1110"></a> *
<a name="line1111"></a> * @param {Array.&lt;string&gt;} keys Parameter names.
<a name="line1112"></a> * @param {Array} values Parameter values.
<a name="line1113"></a> * @param {goog.Uri=} opt_uri URI object that should have its cache
<a name="line1114"></a> *     invalidated when this object updates.
<a name="line1115"></a> * @param {boolean=} opt_ignoreCase If true, ignore the case of the parameter
<a name="line1116"></a> *     name in #get.
<a name="line1117"></a> * @return {!goog.Uri.QueryData} The populated query data instance.
<a name="line1118"></a> */
<a name="line1119"></a>goog.Uri.QueryData.createFromKeysValues = function(
<a name="line1120"></a>    keys, values, opt_uri, opt_ignoreCase) {
<a name="line1121"></a>  if (keys.length != values.length) {
<a name="line1122"></a>    throw Error(&#39;Mismatched lengths for keys/values&#39;);
<a name="line1123"></a>  }
<a name="line1124"></a>  var queryData = new goog.Uri.QueryData(null, null, opt_ignoreCase);
<a name="line1125"></a>  for (var i = 0; i &lt; keys.length; i++) {
<a name="line1126"></a>    queryData.add(keys[i], values[i]);
<a name="line1127"></a>  }
<a name="line1128"></a>  return queryData;
<a name="line1129"></a>};
<a name="line1130"></a>
<a name="line1131"></a>
<a name="line1132"></a>/**
<a name="line1133"></a> * The map containing name/value or name/array-of-values pairs.
<a name="line1134"></a> * May be null if it requires parsing from the query string.
<a name="line1135"></a> *
<a name="line1136"></a> * We need to use a Map because we cannot guarantee that the key names will
<a name="line1137"></a> * not be problematic for IE.
<a name="line1138"></a> *
<a name="line1139"></a> * @type {goog.structs.Map}
<a name="line1140"></a> * @private
<a name="line1141"></a> */
<a name="line1142"></a>goog.Uri.QueryData.prototype.keyMap_ = null;
<a name="line1143"></a>
<a name="line1144"></a>
<a name="line1145"></a>/**
<a name="line1146"></a> * The number of params, or null if it requires computing.
<a name="line1147"></a> * @type {?number}
<a name="line1148"></a> * @private
<a name="line1149"></a> */
<a name="line1150"></a>goog.Uri.QueryData.prototype.count_ = null;
<a name="line1151"></a>
<a name="line1152"></a>
<a name="line1153"></a>/**
<a name="line1154"></a> * @return {?number} The number of parameters.
<a name="line1155"></a> */
<a name="line1156"></a>goog.Uri.QueryData.prototype.getCount = function() {
<a name="line1157"></a>  this.ensureKeyMapInitialized_();
<a name="line1158"></a>  return this.count_;
<a name="line1159"></a>};
<a name="line1160"></a>
<a name="line1161"></a>
<a name="line1162"></a>/**
<a name="line1163"></a> * Adds a key value pair.
<a name="line1164"></a> * @param {string} key Name.
<a name="line1165"></a> * @param {*} value Value.
<a name="line1166"></a> * @return {!goog.Uri.QueryData} Instance of this object.
<a name="line1167"></a> */
<a name="line1168"></a>goog.Uri.QueryData.prototype.add = function(key, value) {
<a name="line1169"></a>  this.ensureKeyMapInitialized_();
<a name="line1170"></a>  this.invalidateCache_();
<a name="line1171"></a>
<a name="line1172"></a>  key = this.getKeyName_(key);
<a name="line1173"></a>  var values = this.keyMap_.get(key);
<a name="line1174"></a>  if (!values) {
<a name="line1175"></a>    this.keyMap_.set(key, (values = []));
<a name="line1176"></a>  }
<a name="line1177"></a>  values.push(value);
<a name="line1178"></a>  this.count_++;
<a name="line1179"></a>  return this;
<a name="line1180"></a>};
<a name="line1181"></a>
<a name="line1182"></a>
<a name="line1183"></a>/**
<a name="line1184"></a> * Removes all the params with the given key.
<a name="line1185"></a> * @param {string} key Name.
<a name="line1186"></a> * @return {boolean} Whether any parameter was removed.
<a name="line1187"></a> */
<a name="line1188"></a>goog.Uri.QueryData.prototype.remove = function(key) {
<a name="line1189"></a>  this.ensureKeyMapInitialized_();
<a name="line1190"></a>
<a name="line1191"></a>  key = this.getKeyName_(key);
<a name="line1192"></a>  if (this.keyMap_.containsKey(key)) {
<a name="line1193"></a>    this.invalidateCache_();
<a name="line1194"></a>
<a name="line1195"></a>    // Decrement parameter count.
<a name="line1196"></a>    this.count_ -= this.keyMap_.get(key).length;
<a name="line1197"></a>    return this.keyMap_.remove(key);
<a name="line1198"></a>  }
<a name="line1199"></a>  return false;
<a name="line1200"></a>};
<a name="line1201"></a>
<a name="line1202"></a>
<a name="line1203"></a>/**
<a name="line1204"></a> * Clears the parameters.
<a name="line1205"></a> */
<a name="line1206"></a>goog.Uri.QueryData.prototype.clear = function() {
<a name="line1207"></a>  this.invalidateCache_();
<a name="line1208"></a>  this.keyMap_ = null;
<a name="line1209"></a>  this.count_ = 0;
<a name="line1210"></a>};
<a name="line1211"></a>
<a name="line1212"></a>
<a name="line1213"></a>/**
<a name="line1214"></a> * @return {boolean} Whether we have any parameters.
<a name="line1215"></a> */
<a name="line1216"></a>goog.Uri.QueryData.prototype.isEmpty = function() {
<a name="line1217"></a>  this.ensureKeyMapInitialized_();
<a name="line1218"></a>  return this.count_ == 0;
<a name="line1219"></a>};
<a name="line1220"></a>
<a name="line1221"></a>
<a name="line1222"></a>/**
<a name="line1223"></a> * Whether there is a parameter with the given name
<a name="line1224"></a> * @param {string} key The parameter name to check for.
<a name="line1225"></a> * @return {boolean} Whether there is a parameter with the given name.
<a name="line1226"></a> */
<a name="line1227"></a>goog.Uri.QueryData.prototype.containsKey = function(key) {
<a name="line1228"></a>  this.ensureKeyMapInitialized_();
<a name="line1229"></a>  key = this.getKeyName_(key);
<a name="line1230"></a>  return this.keyMap_.containsKey(key);
<a name="line1231"></a>};
<a name="line1232"></a>
<a name="line1233"></a>
<a name="line1234"></a>/**
<a name="line1235"></a> * Whether there is a parameter with the given value.
<a name="line1236"></a> * @param {*} value The value to check for.
<a name="line1237"></a> * @return {boolean} Whether there is a parameter with the given value.
<a name="line1238"></a> */
<a name="line1239"></a>goog.Uri.QueryData.prototype.containsValue = function(value) {
<a name="line1240"></a>  // NOTE(arv): This solution goes through all the params even if it was the
<a name="line1241"></a>  // first param. We can get around this by not reusing code or by switching to
<a name="line1242"></a>  // iterators.
<a name="line1243"></a>  var vals = this.getValues();
<a name="line1244"></a>  return goog.array.contains(vals, value);
<a name="line1245"></a>};
<a name="line1246"></a>
<a name="line1247"></a>
<a name="line1248"></a>/**
<a name="line1249"></a> * Returns all the keys of the parameters. If a key is used multiple times
<a name="line1250"></a> * it will be included multiple times in the returned array
<a name="line1251"></a> * @return {!Array.&lt;string&gt;} All the keys of the parameters.
<a name="line1252"></a> */
<a name="line1253"></a>goog.Uri.QueryData.prototype.getKeys = function() {
<a name="line1254"></a>  this.ensureKeyMapInitialized_();
<a name="line1255"></a>  // We need to get the values to know how many keys to add.
<a name="line1256"></a>  var vals = /** @type {Array.&lt;Array|*&gt;} */ (this.keyMap_.getValues());
<a name="line1257"></a>  var keys = this.keyMap_.getKeys();
<a name="line1258"></a>  var rv = [];
<a name="line1259"></a>  for (var i = 0; i &lt; keys.length; i++) {
<a name="line1260"></a>    var val = vals[i];
<a name="line1261"></a>    for (var j = 0; j &lt; val.length; j++) {
<a name="line1262"></a>      rv.push(keys[i]);
<a name="line1263"></a>    }
<a name="line1264"></a>  }
<a name="line1265"></a>  return rv;
<a name="line1266"></a>};
<a name="line1267"></a>
<a name="line1268"></a>
<a name="line1269"></a>/**
<a name="line1270"></a> * Returns all the values of the parameters with the given name. If the query
<a name="line1271"></a> * data has no such key this will return an empty array. If no key is given
<a name="line1272"></a> * all values wil be returned.
<a name="line1273"></a> * @param {string=} opt_key The name of the parameter to get the values for.
<a name="line1274"></a> * @return {!Array} All the values of the parameters with the given name.
<a name="line1275"></a> */
<a name="line1276"></a>goog.Uri.QueryData.prototype.getValues = function(opt_key) {
<a name="line1277"></a>  this.ensureKeyMapInitialized_();
<a name="line1278"></a>  var rv = [];
<a name="line1279"></a>  if (opt_key) {
<a name="line1280"></a>    if (this.containsKey(opt_key)) {
<a name="line1281"></a>      rv = goog.array.concat(rv, this.keyMap_.get(this.getKeyName_(opt_key)));
<a name="line1282"></a>    }
<a name="line1283"></a>  } else {
<a name="line1284"></a>    // Return all values.
<a name="line1285"></a>    var values = /** @type {Array.&lt;Array|*&gt;} */ (this.keyMap_.getValues());
<a name="line1286"></a>    for (var i = 0; i &lt; values.length; i++) {
<a name="line1287"></a>      rv = goog.array.concat(rv, values[i]);
<a name="line1288"></a>    }
<a name="line1289"></a>  }
<a name="line1290"></a>  return rv;
<a name="line1291"></a>};
<a name="line1292"></a>
<a name="line1293"></a>
<a name="line1294"></a>/**
<a name="line1295"></a> * Sets a key value pair and removes all other keys with the same value.
<a name="line1296"></a> *
<a name="line1297"></a> * @param {string} key Name.
<a name="line1298"></a> * @param {*} value Value.
<a name="line1299"></a> * @return {!goog.Uri.QueryData} Instance of this object.
<a name="line1300"></a> */
<a name="line1301"></a>goog.Uri.QueryData.prototype.set = function(key, value) {
<a name="line1302"></a>  this.ensureKeyMapInitialized_();
<a name="line1303"></a>  this.invalidateCache_();
<a name="line1304"></a>
<a name="line1305"></a>  // TODO(user): This could be better written as
<a name="line1306"></a>  // this.remove(key), this.add(key, value), but that would reorder
<a name="line1307"></a>  // the key (since the key is first removed and then added at the
<a name="line1308"></a>  // end) and we would have to fix unit tests that depend on key
<a name="line1309"></a>  // ordering.
<a name="line1310"></a>  key = this.getKeyName_(key);
<a name="line1311"></a>  if (this.containsKey(key)) {
<a name="line1312"></a>    this.count_ -= this.keyMap_.get(key).length;
<a name="line1313"></a>  }
<a name="line1314"></a>  this.keyMap_.set(key, [value]);
<a name="line1315"></a>  this.count_++;
<a name="line1316"></a>  return this;
<a name="line1317"></a>};
<a name="line1318"></a>
<a name="line1319"></a>
<a name="line1320"></a>/**
<a name="line1321"></a> * Returns the first value associated with the key. If the query data has no
<a name="line1322"></a> * such key this will return undefined or the optional default.
<a name="line1323"></a> * @param {string} key The name of the parameter to get the value for.
<a name="line1324"></a> * @param {*=} opt_default The default value to return if the query data
<a name="line1325"></a> *     has no such key.
<a name="line1326"></a> * @return {*} The first string value associated with the key, or opt_default
<a name="line1327"></a> *     if there&#39;s no value.
<a name="line1328"></a> */
<a name="line1329"></a>goog.Uri.QueryData.prototype.get = function(key, opt_default) {
<a name="line1330"></a>  var values = key ? this.getValues(key) : [];
<a name="line1331"></a>  if (goog.Uri.preserveParameterTypesCompatibilityFlag) {
<a name="line1332"></a>    return values.length &gt; 0 ? values[0] : opt_default;
<a name="line1333"></a>  } else {
<a name="line1334"></a>    return values.length &gt; 0 ? String(values[0]) : opt_default;
<a name="line1335"></a>  }
<a name="line1336"></a>};
<a name="line1337"></a>
<a name="line1338"></a>
<a name="line1339"></a>/**
<a name="line1340"></a> * Sets the values for a key. If the key already exists, this will
<a name="line1341"></a> * override all of the existing values that correspond to the key.
<a name="line1342"></a> * @param {string} key The key to set values for.
<a name="line1343"></a> * @param {Array} values The values to set.
<a name="line1344"></a> */
<a name="line1345"></a>goog.Uri.QueryData.prototype.setValues = function(key, values) {
<a name="line1346"></a>  this.remove(key);
<a name="line1347"></a>
<a name="line1348"></a>  if (values.length &gt; 0) {
<a name="line1349"></a>    this.invalidateCache_();
<a name="line1350"></a>    this.keyMap_.set(this.getKeyName_(key), goog.array.clone(values));
<a name="line1351"></a>    this.count_ += values.length;
<a name="line1352"></a>  }
<a name="line1353"></a>};
<a name="line1354"></a>
<a name="line1355"></a>
<a name="line1356"></a>/**
<a name="line1357"></a> * @return {string} Encoded query string.
<a name="line1358"></a> * @override
<a name="line1359"></a> */
<a name="line1360"></a>goog.Uri.QueryData.prototype.toString = function() {
<a name="line1361"></a>  if (this.encodedQuery_) {
<a name="line1362"></a>    return this.encodedQuery_;
<a name="line1363"></a>  }
<a name="line1364"></a>
<a name="line1365"></a>  if (!this.keyMap_) {
<a name="line1366"></a>    return &#39;&#39;;
<a name="line1367"></a>  }
<a name="line1368"></a>
<a name="line1369"></a>  var sb = [];
<a name="line1370"></a>
<a name="line1371"></a>  // In the past, we use this.getKeys() and this.getVals(), but that
<a name="line1372"></a>  // generates a lot of allocations as compared to simply iterating
<a name="line1373"></a>  // over the keys.
<a name="line1374"></a>  var keys = this.keyMap_.getKeys();
<a name="line1375"></a>  for (var i = 0; i &lt; keys.length; i++) {
<a name="line1376"></a>    var key = keys[i];
<a name="line1377"></a>    var encodedKey = goog.string.urlEncode(key);
<a name="line1378"></a>    var val = this.getValues(key);
<a name="line1379"></a>    for (var j = 0; j &lt; val.length; j++) {
<a name="line1380"></a>      var param = encodedKey;
<a name="line1381"></a>      // Ensure that null and undefined are encoded into the url as
<a name="line1382"></a>      // literal strings.
<a name="line1383"></a>      if (val[j] !== &#39;&#39;) {
<a name="line1384"></a>        param += &#39;=&#39; + goog.string.urlEncode(val[j]);
<a name="line1385"></a>      }
<a name="line1386"></a>      sb.push(param);
<a name="line1387"></a>    }
<a name="line1388"></a>  }
<a name="line1389"></a>
<a name="line1390"></a>  return this.encodedQuery_ = sb.join(&#39;&amp;&#39;);
<a name="line1391"></a>};
<a name="line1392"></a>
<a name="line1393"></a>
<a name="line1394"></a>/**
<a name="line1395"></a> * @return {string} Decoded query string.
<a name="line1396"></a> */
<a name="line1397"></a>goog.Uri.QueryData.prototype.toDecodedString = function() {
<a name="line1398"></a>  return goog.Uri.decodeOrEmpty_(this.toString());
<a name="line1399"></a>};
<a name="line1400"></a>
<a name="line1401"></a>
<a name="line1402"></a>/**
<a name="line1403"></a> * Invalidate the cache.
<a name="line1404"></a> * @private
<a name="line1405"></a> */
<a name="line1406"></a>goog.Uri.QueryData.prototype.invalidateCache_ = function() {
<a name="line1407"></a>  this.encodedQuery_ = null;
<a name="line1408"></a>};
<a name="line1409"></a>
<a name="line1410"></a>
<a name="line1411"></a>/**
<a name="line1412"></a> * Removes all keys that are not in the provided list. (Modifies this object.)
<a name="line1413"></a> * @param {Array.&lt;string&gt;} keys The desired keys.
<a name="line1414"></a> * @return {!goog.Uri.QueryData} a reference to this object.
<a name="line1415"></a> */
<a name="line1416"></a>goog.Uri.QueryData.prototype.filterKeys = function(keys) {
<a name="line1417"></a>  this.ensureKeyMapInitialized_();
<a name="line1418"></a>  goog.structs.forEach(this.keyMap_,
<a name="line1419"></a>      /** @this {goog.Uri.QueryData} */
<a name="line1420"></a>      function(value, key, map) {
<a name="line1421"></a>        if (!goog.array.contains(keys, key)) {
<a name="line1422"></a>          this.remove(key);
<a name="line1423"></a>        }
<a name="line1424"></a>      }, this);
<a name="line1425"></a>  return this;
<a name="line1426"></a>};
<a name="line1427"></a>
<a name="line1428"></a>
<a name="line1429"></a>/**
<a name="line1430"></a> * Clone the query data instance.
<a name="line1431"></a> * @return {!goog.Uri.QueryData} New instance of the QueryData object.
<a name="line1432"></a> */
<a name="line1433"></a>goog.Uri.QueryData.prototype.clone = function() {
<a name="line1434"></a>  var rv = new goog.Uri.QueryData();
<a name="line1435"></a>  rv.encodedQuery_ = this.encodedQuery_;
<a name="line1436"></a>  if (this.keyMap_) {
<a name="line1437"></a>    rv.keyMap_ = this.keyMap_.clone();
<a name="line1438"></a>    rv.count_ = this.count_;
<a name="line1439"></a>  }
<a name="line1440"></a>  return rv;
<a name="line1441"></a>};
<a name="line1442"></a>
<a name="line1443"></a>
<a name="line1444"></a>/**
<a name="line1445"></a> * Helper function to get the key name from a JavaScript object. Converts
<a name="line1446"></a> * the object to a string, and to lower case if necessary.
<a name="line1447"></a> * @private
<a name="line1448"></a> * @param {*} arg The object to get a key name from.
<a name="line1449"></a> * @return {string} valid key name which can be looked up in #keyMap_.
<a name="line1450"></a> */
<a name="line1451"></a>goog.Uri.QueryData.prototype.getKeyName_ = function(arg) {
<a name="line1452"></a>  var keyName = String(arg);
<a name="line1453"></a>  if (this.ignoreCase_) {
<a name="line1454"></a>    keyName = keyName.toLowerCase();
<a name="line1455"></a>  }
<a name="line1456"></a>  return keyName;
<a name="line1457"></a>};
<a name="line1458"></a>
<a name="line1459"></a>
<a name="line1460"></a>/**
<a name="line1461"></a> * Ignore case in parameter names.
<a name="line1462"></a> * NOTE: If there are already key/value pairs in the QueryData, and
<a name="line1463"></a> * ignoreCase_ is set to false, the keys will all be lower-cased.
<a name="line1464"></a> * @param {boolean} ignoreCase whether this goog.Uri should ignore case.
<a name="line1465"></a> */
<a name="line1466"></a>goog.Uri.QueryData.prototype.setIgnoreCase = function(ignoreCase) {
<a name="line1467"></a>  var resetKeys = ignoreCase &amp;&amp; !this.ignoreCase_;
<a name="line1468"></a>  if (resetKeys) {
<a name="line1469"></a>    this.ensureKeyMapInitialized_();
<a name="line1470"></a>    this.invalidateCache_();
<a name="line1471"></a>    goog.structs.forEach(this.keyMap_,
<a name="line1472"></a>        /** @this {goog.Uri.QueryData} */
<a name="line1473"></a>        function(value, key) {
<a name="line1474"></a>          var lowerCase = key.toLowerCase();
<a name="line1475"></a>          if (key != lowerCase) {
<a name="line1476"></a>            this.remove(key);
<a name="line1477"></a>            this.setValues(lowerCase, value);
<a name="line1478"></a>          }
<a name="line1479"></a>        }, this);
<a name="line1480"></a>  }
<a name="line1481"></a>  this.ignoreCase_ = ignoreCase;
<a name="line1482"></a>};
<a name="line1483"></a>
<a name="line1484"></a>
<a name="line1485"></a>/**
<a name="line1486"></a> * Extends a query data object with another query data or map like object. This
<a name="line1487"></a> * operates &#39;in-place&#39;, it does not create a new QueryData object.
<a name="line1488"></a> *
<a name="line1489"></a> * @param {...(goog.Uri.QueryData|goog.structs.Map|Object)} var_args The object
<a name="line1490"></a> *     from which key value pairs will be copied.
<a name="line1491"></a> */
<a name="line1492"></a>goog.Uri.QueryData.prototype.extend = function(var_args) {
<a name="line1493"></a>  for (var i = 0; i &lt; arguments.length; i++) {
<a name="line1494"></a>    var data = arguments[i];
<a name="line1495"></a>    goog.structs.forEach(data,
<a name="line1496"></a>        /** @this {goog.Uri.QueryData} */
<a name="line1497"></a>        function(value, key) {
<a name="line1498"></a>          this.add(key, value);
<a name="line1499"></a>        }, this);
<a name="line1500"></a>  }
<a name="line1501"></a>};
</pre>


</body>
</html>
